package com.bnz.test;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * rabbitmq Hello World 模型示例
 * 消息生产者
 */
public class MessageProducer {

    /**
     * 消息队列名称
     */
    public static final String QUEUE_NAME = "SF_To_Idea_DynamicFlightData_Queue_dev2";


    public static void main(String[] args) throws Exception {
        // 连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 设置服务地址
        factory.setUri("amqp://data-ep03-dev.silassz.com:5672");
        // 设置Virtual Hosts
        factory.setVirtualHost("test1");
        // 设置用户名
        factory.setUsername("fengyi_user");
        // 设置密码
        factory.setPassword("Gogogo@2024");
        Connection connection = null;
        Channel channel = null;
        try {
            // 获取连接
            connection = factory.newConnection();
            // 建立通道
            channel = connection.createChannel();
            // 绑定消息队列
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);
            String message = "{\"data\":[{\"Latitude\":227325130,\"OrderID\":\"FZ0000000000031021-20240229-88090000\",\"GS\":300,\"Longitude\":1139468580,\"UASID\":\"UAS31021\",\"TimeStamp\":\"20240229111752\",\"FlightTime\":215,\"Coordinate\":\"CGCS2000\",\"AltitudeDesc\":\"MSL\",\"ManufactureID\":\"91360782MA36YQXUXJ\",\"UavType\":\"3A\",\"Height\":18100,\"Course\":130,\"Altitude\":18102}],\"dataType\":\"DynamicFlightInfo\",\"dataSource\":\"SF\"}";
            // 发布消息 (hello world 消息模型无需指定交换机类型)
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [Producer(hello world)] Sent '" + message + "'");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            if (channel != null) {
                channel.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}